package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sql.SprintViewDao;
import beans.SprintItemBean;

/**
 * Servlet implementation class EditSprint
 */
@WebServlet("/EditSprint")
public class EditSprint extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditSprint() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SprintItemBean sprintItemBean = new SprintItemBean();
		sprintItemBean.setName(request.getParameter("sprint-name"));
		sprintItemBean.setDescription(request.getParameter("sprint-description"));
		sprintItemBean.setStartDate(request.getParameter("sprint-start-date"));
		sprintItemBean.setEndDate(request.getParameter("sprint-end-date"));

		try {
			sprintItemBean.setId(Integer.parseInt(request.getParameter("sprint-id")));
		}
		catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		SprintViewDao sprintViewDao = new SprintViewDao();
		try {
			sprintViewDao.update(sprintItemBean);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		
		// return data
		PrintWriter out = response.getWriter();
		out.write(sprintItemBean.getJson());
	}

}
